<template>
  <el-dialog
      v-model="centerDialogVisible"
      :title="formTitle"
      width="25%"
      destroy-on-close
      center
      @close="dialogClose">
    <el-form
        ref="ruleFormRef"
        label-position="top"
        label-width="100px"
        :model="formLabelAlign"
        :rules="rules">
      <el-form-item label="代理商名称"
                    prop="agentName">
        <el-input v-model="formLabelAlign.agentName"></el-input>
      </el-form-item>
      <el-form-item label="代理商负责人" prop="agentPrincipal">
        <el-input v-model="formLabelAlign.agentPrincipal"></el-input>
      </el-form-item>
      <el-form-item label="电话号码" prop="phoneNumber">
        <el-input v-model="formLabelAlign.phoneNumber"></el-input>
      </el-form-item>
      <el-form-item
          label="地址" prop="companyAddr">
        <el-input v-model="formLabelAlign.companyAddr"></el-input>
      </el-form-item>
    </el-form>
    <div class="bottomButton">
      <el-button class="affirm" @click="resetForm" style="width: 47.5%;height: 100%;">确认</el-button>
      <el-button class="cancel" @click="centerDialogVisible = false" style="width: 47.5%;height: 100%;">取消</el-button>
    </div>
  </el-dialog>

</template>

<script>
import {isvalidPhone} from "@/utils/public";

let me;
let validPhone = (rule, value, callback) => {//验证手机号是否合法
  if (value && !isvalidPhone(value)) {
    callback(new Error("请输入正确的11位手机号码"));
  } else {
    callback();
  }
};
export default {
  name: "EditAgentDialog",
  data() {
    return {
      formTitle: '',
      formLabelAlign: {
        companyAddr: "",
        phoneNumber: "",
        agentPrincipal: "",
        agentName: ""
      },
      rules: {
        companyAddr: [
          {required: true, message: '不能为空', trigger: 'blur'},
        ],
        agentPrincipal: [
          {required: true, message: '不能为空', trigger: 'blur'},
        ],
        agentName: [
          {required: true, message: '不能为空', trigger: 'blur'},
        ],
        phoneNumber: [
          {required: true, message: '不能为空', trigger: 'blur'},
          {trigger: 'blur', validator: validPhone}//这里需要用到全局变量
        ]
      },
      centerDialogVisible: false
    }
  },
  methods: {
    showDialog(row) {
      me = this;
      this.centerDialogVisible = true;
      //查询下拉框
      this.$nextTick(() => {
        this.formTitle = '新增代理商信息';
        me.formLabelAlign.actionType = 'agentAdd';
        if (row) {
          me.formLabelAlign = JSON.parse(JSON.stringify(row));
          this.formTitle = '编辑代理商信息';
          me.formLabelAlign.actionType = 'agentUpdate';
        }
      })
    },
    resetForm() {
      me.$refs.ruleFormRef.validate(async (valid, fields) => {
        if (valid) {
          let response = await me.$asyncPost("agent", me.formLabelAlign);
          if (response.success) {
            me.$message.success('添加成功');
            me.centerDialogVisible = false;
            me.$parent.dataQuery();
          }
        } else {
          console.log('error submit!', fields)
        }
      })
    },
    dialogClose() {
      this.$refs.ruleFormRef.resetFields();//关闭的时候一定要清空之前的form表单已填写的数据！！！！！
    }
  }
}
</script>

<style scoped lang="scss">

</style>